package com.nickstumpos.android.easyVolume.scheduler.controller;

import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;

import com.nickstumpos.android.easyVolume.constants.EVCONSTANTS;
import com.nickstumpos.android.easyVolume.scheduler.dto.SchedulingInfoDto;
import com.nickstumpos.android.easyVolume.scheduler.reciever.ApplyPresetReciever;
import com.nickstumpos.android.easyVolume.scheduler.util.SchedulingUtil;

public class SchedulePresetController {

	private Context				context;
	private AlarmManager	am;

	public SchedulePresetController(Context context) {
		this.context = context;
		this.am = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);

	}

	public void removeScheduledPreset(int code) {
		PendingIntent presetSender = PendingIntent.getBroadcast(this.context, code,
				new Intent(this.context, ApplyPresetReciever.class), 0);
		this.am.cancel(presetSender);
	}

	public void schedulePreset(SchedulingInfoDto schedule) {
		Intent intent = new Intent(this.context, ApplyPresetReciever.class);
		intent.putExtra(EVCONSTANTS.SCHEDULE, schedule);
		PendingIntent presetSender = PendingIntent.getBroadcast(this.context,
				schedule.getCode(), intent, 0);

		this.am.set(AlarmManager.RTC_WAKEUP, SchedulingUtil
				.findNextRunTime(schedule), presetSender);
	}
}
